﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quick.CodeGenerator.Data
{
    public abstract class IDataBase
    {
        /// <summary>
        /// 数据库链接字符串
        /// </summary>
        protected string m_ConnectonString;
        /// <summary>
        /// 设置链接字符串
        /// </summary>
        /// <param name="connectionString"></param>
        public void SetConnection(string connectionString)
        {
            m_ConnectonString = connectionString;
        }
        /// <summary>
        /// 表字段名称
        /// </summary>
        public abstract string TableName { get; }
        /// <summary>
        /// 根据关键字,获取指定的表名
        /// </summary>
        /// <param name="keyWords"></param>
        /// <returns></returns>
        public abstract List<Model.TableInfo> GetTableListInfo(string keyWords);
        /// <summary>
        /// 获取指定表名的列数据
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public abstract List<Model.ColumnInfo> GetColumnInfoList(string tableName);
        /// <summary>
        /// 创建表查询SQL的where 语句
        /// </summary>
        /// <param name="keyWords"></param>
        /// <param name="sqlTable"></param>
        /// <returns></returns>
        protected string CreateTableInfoSqlWhere(string keyWords)
        {
            string sqlWhere = "";
            if (!string.IsNullOrEmpty(keyWords) && keyWords.Trim().Length > 0)
            {
                string[] kws = keyWords.Split(new char[] { ',', ' ', '/', ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string k in kws)
                {
                    if (!string.IsNullOrEmpty(sqlWhere))
                    {
                        sqlWhere += " OR ";
                    }
                    sqlWhere += $" {this.TableName} LIKE '%{k}%' ";
                }
            }
            return sqlWhere;
        }
        /// <summary>
        /// 转换各数据库的字段类型
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public List<Model.ColumnInfo> TransfromDbType2DbParameterType(List<Model.ColumnInfo> models)
        {
            if (models == null || models.Count == 0)
            {
                return models;
            }
            for (int i = 0; i < models.Count; i++)
            {
                models[i].CSharpColumnDBType = TransfromDbType2DbParameterType(models[i].DATA_TYPE, models[i].DATA_SCALE, models[i].DATA_LENGTH);
                models[i].CSharpColumnType = TransfromDbType2CCsharp(models[i].DATA_TYPE, models[i].DATA_SCALE, models[i].DATA_LENGTH, models[i].NULLABLE);
            }
            return models;
        }
        /// <summary>
        /// 转换各数据库的字段类型
        /// </summary>
        /// <param name="type">栏位类型</param>
        /// <param name="decimalStr">数据小数点精度</param>
        /// <param name="length">栏位长度</param>
        /// <returns></returns>
        public abstract string TransfromDbType2DbParameterType(string type, string decimalStr, string length);
        /// <summary>
        /// 根据查询的数据类型转换成C#数据类型
        /// </summary>
        /// <param name="type">数据类型</param>
		/// <param name="decimalStr">是否是小数</param>
        /// <param name="length">length</param>
		/// <param name="canNullStr">能否为空</param>
        /// <returns></returns>
        public abstract string TransfromDbType2CCsharp(string type, string decimalStr, string length, string canNullStr);
    }
}
